import os,re,subprocess
from main import probe
__author__ = 'wuyou'

wifimac = '00:26:66:47:9e:a4'

# get local blue addr
def local_blue_addr():
    result = os.popen('hcitool dev')
    res = result.read()
    mac = "[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}"
    aa = re.findall(mac,res)[0]
    #print aa
    return aa

# get local blue dev
def local_blue_dev():
    result = os.popen('hcitool dev')
    res = result.read()
    mac = "hci\d"
    aa = re.findall(mac,res)[0]
    #print aa
    return aa

def CVE1782(mac):
    cmd = './bnep '+mac
    result = os.popen(cmd)
    res = result.read()
    print res
    mac = "[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}"
    aa = re.findall(mac, res)
    print aa,len(aa),type(aa)
    if len(aa):
        #print type(aa),aa
        dd = [[aa[0].upper(),'CVE-2017-1782']]
        probe.do_log_for_blueleak(dd)
        return dd
'''
  wifi leak test CVE-2019-3846
'''
def getCVE3846():
    #print getMarvenMAc(),type(getMarvenMAc())
    cmd = subprocess.Popen('dmesg -wH', stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
    code = 'Code:'
    while True:
        data = cmd.stdout.readline()
        print data
        aa = re.search(code,data)
        if aa:
            dd = ['94:ba:56:00:cb:ba','CVE-2019-3846']
            probe.do_log_for_wifileak(dd)
            print '-----------------------------------------------------',dd
            #return dd
def getMarvenMAc():
    cmd = subprocess.Popen('ifconfig | grep wlx',stdin=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
    mac = "[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}"
    data = cmd.stdout.read()
   # print data
    aa = re.findall(mac,data)
    print aa
    if wifimac in aa:
        aa.remove('00:26:66:47:9e:a4')
    print aa[0]
    return aa[0]



if __name__ == '__main__':
    # blue leak 3
    #aa = CVE1782('22:22:82:8e:ca:64')
    # wifi leak 1
    getCVE3846()
    #getMarvenMAc()
   # print aa